package com.linkq.ipinyin.service;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.linkq.core.base.BaseService;
import com.linkq.ipinyin.common.constants.GlobalData;
import com.linkq.ipinyin.dao.CustomerDao;
import com.linkq.ipinyin.mapper.CustomerMapper;
import com.linkq.ipinyin.model.Customer;
import com.linkq.ipinyin.vo.ResultInfo;
import com.xiaoleilu.hutool.bean.BeanUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @FileName CustomerService
 * @Description
 * @Author wangpeng
 * @date 2024-12-10
 **/

@Service
@Transactional
public class CustomerService extends BaseService<CustomerDao, Customer> {
    @Autowired
    private CustomerMapper customerMapper;

    public ResultInfo getUserByAppleAppId(String appleAppId) {
        Customer customer = selectOne(
                Wrappers.<Customer>lambdaQuery()
                        .eq(Customer::getAppleAppId, appleAppId)
        );
        return ResultInfo.success(customer == null ? 0 : 1);
    }

    public ResultInfo register(Customer customer) {
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(customer.getMobile()), Customer::getMobile, customer.getMobile());
        wrapper.eq(StringUtils.isNotBlank(customer.getNickName()), Customer::getNickName, customer.getNickName());
        wrapper.eq(StringUtils.isNotBlank(customer.getIdCardNo()), Customer::getIdCardNo, customer.getIdCardNo());
        wrapper.eq(StringUtils.isNotBlank(customer.getAppleAppId()), Customer::getAppleAppId, customer.getAppleAppId());
        List<Customer> customerList = customerMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(customerList)) {
            return ResultInfo.error("用户已存在", -2);
        }
        customer.setVipType(0);
        if (customerMapper.insert(customer) > 0) {
            return ResultInfo.success(customer);
        }
        return ResultInfo.error("注册失败", -1);
    }

    public SaTokenInfo login(Customer customer) {
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(customer.getAppleAppId())) {
            wrapper.eq(Customer::getAppleAppId, customer.getAppleAppId());
        } else {
            wrapper.eq(StringUtils.isNotBlank(customer.getPassword()),Customer::getPassword, customer.getPassword());
            wrapper.eq(Customer::getStatus, GlobalData.CUSTOMER_STATUS_NORMAL);
            wrapper.and(w -> {
                w.eq(StringUtils.isNotBlank(customer.getNickName()),Customer::getNickName, customer.getNickName())
                        .or()
                        .eq(StringUtils.isNotBlank(customer.getMobile()), Customer::getMobile, customer.getMobile())
                        .or()
                        .eq(StringUtils.isNotBlank(customer.getIdCardNo()), Customer::getIdCardNo, customer.getIdCardNo());
            });
        }
        Customer cus= customerMapper.selectOne(wrapper);
        if (Objects.isNull(cus)) {
            return null;
        }
        StpUtil.login(cus.getId());
        SaTokenInfo token = StpUtil.getTokenInfo();
        StpUtil.getSession().set(cus.getId().toString(), cus);
        return token;
    }

    /**
     * 获取当前登录用户信息
     */
    public Customer getUser() {
        return (Customer) StpUtil.getSession().get(StpUtil.getLoginIdByToken(StpUtil.getTokenValue()).toString());
    }


    public ResultInfo edit(Customer customer) {
        Customer origin = customerMapper.selectById(customer.getId());
        if (Objects.isNull(origin)) {
            ResultInfo.error("用户不存在", -1);
        }
        BeanUtil.copyProperties(customer, origin);
        if (customerMapper.updateById(origin) > 0) {
            return ResultInfo.success(1, "更新成功");
        }
        return ResultInfo.error("更新用户失败", -2);
    }


    public ResultInfo editPassword(Customer cus) {
        String mobile = cus.getMobile();
        String password = cus.getPassword();
        Customer customer = customerMapper.selectOne(
                Wrappers.<Customer>lambdaQuery()
                        .eq(Customer::getMobile, mobile)
                        .eq(Customer::getStatus, GlobalData.CUSTOMER_STATUS_NORMAL)
        );
        if (Objects.isNull(customer)) {
            return ResultInfo.error("账号不存在", -1);
        }
        customer.setPassword(password);
        if (customerMapper.updateById(customer) > 0) {
            return ResultInfo.success("修改密码成功");
        } else {
            return ResultInfo.error("修改密码失败", -2);
        }
    }

    public void logOut() {
        StpUtil.logoutByTokenValue(StpUtil.getTokenValue());
    }
}
